﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class WMS_T_OutStorckRMRowDetailsDAL : BaseSqlDBDAL<WMS_T_OutStockRMRowDetails>
    {
  

        public List<WMS_T_OutStockRMRowDetails> GetPRUpdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_OutStockRMRowDetails WHERE   Status='8' and billno in (select billno from wms_T_OutStockRM where (billtype='领料出库' or billtype='非拉动领料出库')) ";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }


      

        public List<WMS_T_OutStockRMRowDetails> GetPRUpBOMdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_OutStockRMRowDetails WHERE   Status='8' and billno in (select billno from wms_T_OutStockRM where billtype='领料出库') ";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }

        public DataTable getPBackUpdate()
        {
             
            string sql = @"SELECT * FROM dbo.wms_T_OutReturn WHERE   Status='0'";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

          
            return dt;
        }


        public DataTable getUpdateInvernAll(DataTable dt)
        {

            string sqlWhere = " and (";
            foreach (DataRow dr in dt.Rows)
            {
                sqlWhere = sqlWhere + string.Format(" (PartNumber='{0}' and Storage_SN='{1}') or", dr["T$ITEM"].ToString(), dr["T$CWAR"].ToString());
            }
            sqlWhere = sqlWhere.Substring(0, sqlWhere.Length - 2) + ") ";
            string sql = @"SELECT * FROM dbo.wms_T_Inventory WHERE 1=1 "+sqlWhere+"  ORDER BY DateCode,EnterTime ";
            var dtR = SqlDBHelper.GetDataSet(sql).Tables[0];


            return dtR;
        }



        public int UpdateInvernAll(List<WMS_T_Inventory> updateList, List<WMS_T_Inventory> deleteList)
        {
            string sql = string.Empty;
            foreach (WMS_T_Inventory UpdateEntity in updateList)
            {
                sql = sql + string.Format("update wms_T_Inventory set Qty='{0}' where Guid='{1}';", UpdateEntity.Qty, UpdateEntity.Guid);
            }
            foreach (WMS_T_Inventory deleteEntity in deleteList)
            {
                sql = sql + string.Format("delete wms_T_Inventory  where Guid='{0}';", deleteEntity.Guid);
            }
            var dtR = SqlDBHelper.ExcuteSQL(sql);
            return dtR;
        }


        public int UpdateOutStockRows(List<tddev102> list)
        {
            string sql = string.Empty;
            foreach (tddev102 UpdateEntity in list)
            {
                sql = sql + string.Format("update wms_T_OutStockRMRows set Status='9' where RelatedBill='{0}' and RowNo='{1}' and OrderNo='{2}';", UpdateEntity.orno,UpdateEntity.pono, UpdateEntity.sqnb);
            }
            
            var dtR = SqlDBHelper.ExcuteSQL(sql);
            return dtR;
        }
    }
}
